package com.by.goods.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.common.core.utils.Assert;
import com.by.goods.entity.*;
import com.by.goods.mapper.*;
import com.by.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;

@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    /**
     * 商品详情
     */
    @Autowired
    SpuDetailMapper spuDetailMapper;

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SkuStockMapper skuStockMapper;

    @Autowired
    SpuExtensionMapper spuExtensionMapper;

    @Autowired
    AttrMapper attrMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    AttrCategoryMapper attrCategoryMapper;

    @Autowired
    SpuMapper spuMapper;



    @Transactional
    public boolean save(Spu entity){
        //1.保存spu
        int rs=getBaseMapper().insert(entity);
        Assert.isZero(rs,"保存spu失败");
        //2.保存详情
        SpuDetail detail=new SpuDetail();
        detail.setDetail(entity.getDetail());
        detail.setSpuId(entity.getSpuId());
        rs=spuDetailMapper.insert(detail);
        Assert.isZero(rs,"保存spu详情失败");

        int total=0;//商品总数量
        //3.保存sku
        if(null!=entity.getSkuList()&&entity.getSkuList().size()>0){
            Iterator<Sku> it=entity.getSkuList().iterator();
            while (it.hasNext()){
                Sku sku=it.next();
                sku.setSpuId(entity.getSpuId());
                //保存sku
                rs=skuMapper.insert(sku);
                Assert.isZero(rs,"保存sku失败");
                //保存该sku的库存
                SkuStock stock=new SkuStock();
                stock.setSkuId(sku.getSkuId());
                stock.setActual_stock(sku.getStock());
                stock.setStock(sku.getStock());
                rs=skuStockMapper.insert(stock);
                Assert.isZero(rs,"保存sku库存失败");
                //累加商品数量
                total+=sku.getStock();
            }
            //保存spu库存
            SpuExtension spuExtension=new SpuExtension();
            spuExtension.setStock(total);
            spuExtension.setActualStock(total);
            spuExtension.setSpuId(entity.getSpuId());
            rs=spuExtensionMapper.insert(spuExtension);
            Assert.isZero(rs,"保存spu库存失败");
        }

        //保存规格
        if(null!=entity.getAttrList()&&entity.getAttrList().size()>0){
            Iterator<Attr> it2=entity.getAttrList().iterator();
            while (it2.hasNext()){
                Attr attr=it2.next();
                rs=attrMapper.insert(attr);
                Assert.isZero(rs,"保存规格失败");

                //保存规格类别
                rs=attrCategoryMapper.save(entity.getCategoryId(), attr.getAttrId());
                Assert.isZero(rs,"保存规格类别失败");

                //保存值
                //遍历规格值
                if(null!=attr.getAttrValueList()&&attr.getAttrValueList().size()>0){
                    Iterator<AttrValue> it3=attr.getAttrValueList().iterator();
                    while (it3.hasNext()){
                        AttrValue attrValue=it3.next();
                        attrValue.setAttrId(attr.getAttrId());
                        rs=attrValueMapper.insert(attrValue);
                        Assert.isZero(rs,"保存规格值失败");

                        //成功了组合spu规格属性
                        SpuAttrValue spuAttrValue=SpuAttrValue.builder()
                                .attrId(attr.getAttrId())
                                .attrName(attr.getName())
                                .attrValueId(attrValue.getAttrValueId())
                                .attrValueName(attrValue.getValue())
                                .spuId(entity.getSpuId())
                                .build();
                        rs=spuAttrValueMapper.insert(spuAttrValue);
                        Assert.isZero(rs,"保存spu规格属性值失败");
                    }
                }
            }
        }
        return true;
    }

    @Override
    public IPage<Spu> querySpuList(int pageNum, int pageSize) {
        Page<Spu> page = new Page<>(pageNum, pageSize);
        // 调用mapper的分页查询方法
        return spuMapper.queryList(page);
    }


    @Override
    public SpuDetail queryById(int spuId) {
        return spuDetailMapper.queryById(spuId);
    }
}
